<?php

/**
 * Description of gui
 *
 * @author MerDars
 */
class gui {

    var $tplPath = "";
    var $currentTpl = null;
    var $tplStore = [];
    var $debug = [];
    function __construct($tplName, $tplPath = null){
        return $this->load($tplName, $tplPath);
    }
    function __toString()
    {
        return (string)$this->get();
    }

    function load($tplName, $tplPath = null){
        if(is_array($tplName))
            $this->tplStore[$this->current(array_keys($tplName)[0])] = file_get_contents($this->tplPath($tplPath) . array_values($tplName)[0] . ".html");
        else
            $this->tplStore[$this->current($tplName)] = file_get_contents($this->tplPath($tplPath) . $tplName . ".html");
        return $this;
    }

    /**
     * adds content to template - replaces selectors with data
     * content = [ selector => data ]
     * @param array $content
     * @param string $tplName optional
     * @return \gui
     */
    function add($content, $tplName = null){
        $this->tplStore[$this->current($tplName)] = str_replace(array_map([$this, "tplNameSelector"], array_keys($content)), array_values($content), $this->tplStore[$this->current($tplName)]);
        return $this;
    }

    /**
     * 
     * @param string $tplName
     * @return \gui
     */
    function remove($tplName = null){
        unset($this->tplStore[$this->current($tplName)]);
        return $this;
    }

    /**
     * returnt template content
     * @param string $tplName template name
     * @return string template content
     */
    function get($tplName = null){
        $this->current($tplName);
        return $this->tplStore[$this->current($tplName)];
    }

    /**
     * return tpl store
     * @return array template store data
     */
    function getTplStore(){
        return $this->tplStore;
    }

    /**
     * returns list of template names
     * @return array template name list
     */
    function getTplNames(){
        return array_keys($this->tplStore);
    }

    /**
     * returns current template name
     * @param string $tplName
     * @return string template name
     */
    function current($tplName = null){
        return $this->currentTpl = is_null($tplName) ? $this->currentTpl : $tplName;
    }

    /**
     * return template path
     * @param string $tplPath
     * @return string template path
     */
    function tplPath($tplPath = null){
        return $this->tplPath = is_null($tplPath) ? $this->tplPath : $tplPath;
    }

    /**
     * "tplname" ==> "{tplname}"
     * @param string $tplName
     * @return string template names formatted string
     */
    function tplNameSelector($tplName){
        return "{" . $tplName . "}";
    }

}

?>
